package com.beautiful.xin.sort;

import org.apache.commons.lang3.StringUtils;

/**
 * @author xuqingxin 2021/9/14 7:51 下午
 */
public class Add36Strings {
    public static void main(String[] args) {
        System.out.println(add36Strings("1b", "2c"));
    }

    public static String add36Strings(String s1, String s2) {
        int l1 = s1.length() - 1;
        int l2 = s2.length() - 1;
        int carry = 0;
        String ans = "";
        while (l1 >= 0 || l2 >= 0 || carry > 0) {
            int n1 = l1 >= 0 ? getInt(s1.charAt(l1)) : 0;
            int n2 = l2 >= 0 ? getInt(s2.charAt(l2)) : 0;
            int t = n1 + n2 + carry;
            ans += getChar(t % 36);
            carry = t / 36;
            l1--;
            l2--;
        }
        return StringUtils.reverse(ans);
    }

    public static char getChar(int n) {
        if (n <= 9) {
            return (char) (n + '0');
        } else {
            return (char) (n - 10 + 'a');
        }
    }

    public static int getInt(char ch) {
        if ('0' <= ch && ch <= '9') {
            return ch - '0';
        } else {
            return ch - 'a' + 10;
        }
    }
}
